/**
 * 
 */
package com.precisosoft.mathematize.server.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;

import com.precisosoft.mathematize.server.domain.User;

/**
 * @author jewettdx
 * 
 */
public class UserDao extends BaseDao {

	public Long createObject(Object object) {
		return null;
	}

	@Override
	public List<Object> retrieveObjects(int maxResults, int firstResult) {
		return null;
	}

	public String createUser(User user) {

		EntityManager em = createEntityManager();
		EntityTransaction tx = em.getTransaction();
		String login = "";

		try {
			tx.begin();
			em.persist(user);
			login = user.getLogin();
			tx.commit();
		} catch (Throwable t) {
			t.printStackTrace();
			tx.rollback();
		} finally {
			em.close();
		}

		return login;
	}

	// e.g. UserDetails loadUserByUsername(String username)

	public User retrieveUser(String login) {

		EntityManager em = createEntityManager();
		User user = null;

		try {
			TypedQuery<User> query = em.createQuery(
					"select u from User u where u.login = ?1", User.class);
			query.setParameter(1, login);
			user = query.getSingleResult();
		} finally {
			em.close();
		}

		return user;
	}

}
